package cn.kgc.ywz.smbms.web.web;

import cn.kgc.ywz.smbms.web.entity.Constant;
import cn.kgc.ywz.smbms.web.entity.User;
import cn.kgc.ywz.smbms.web.service.UserService;
import org.apache.ibatis.mapping.Environment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class LoginController {

    /**
     * 登录页面
     */
    @RequestMapping("/toLogin")
    public String toLogin(String flag) {
        return "login";
    }

    @Autowired
    private UserService userService;

    /**
     * 登录处理
     */
    @RequestMapping("/doLogin")
    public String doLogin(String userCode, String userPassword, HttpSession session) {
        User user = this.userService.findUser(userCode, userPassword);
        if (user == null) {
            return "redirect:toLogin?flag=-1";  //redirect:重定向
        }
        session.setAttribute(Constant.USER, user);
        return "redirect:toFrame";
    }

    /**
    * 首页页面
     */
    @RequestMapping("/toFrame")
    public String toFrame() {
        return "frame";
    }

    /**
     * 登出
     */
    @RequestMapping("/outLogin")
    public String outLogin(HttpSession session) {
        session.invalidate();
        return "redirect:toLogin";
    }
}
